//引入接口
import API from '../../config/server.js';
Page({
  audio:{},
  data:{
    cate:[],
    yxdata:[],
    cateId:0,
    poster:'',
    name:'',
    author:'',
    src:''
  },
  onLoad()
  {
    this.getCate()
  },
  onReady(e)
  {
    //创建一个语音播放的对象
    this.audio = wx.createInnerAudioContext('myAudio',this);
  },
  getCate(){
    let that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: API.voicenav,
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        
        let cate = res.data.data;
        // console.log(cate)
        that.setData({
          cate: cate.dhty_e9,
          cateId: cate.dhty_e9[0].yxid_a7
        });

        that.getDetail();

      },
      fail: function(res) {
        wx.showToast({
          title: '网络错误',
          icon: 'error'
        })
      },
      complete: function(res) {
        wx.hideLoading()  //关闭loading
      },
    })
  },
  getDetail(){
    let that = this;
    let cateid = this.data.cateId;
    wx.request({
      url: API.voicedetail + '?id=' + cateid,
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        let yxdata = res.data.data;
        // console.log(yxdata)
        if (res.data.success){

          let type = Object.prototype.toString.call(yxdata.yxpfyy_fe);  //获取yxpfyy_fe的数据类型
          let arr = [];
          if(/Object/.test(type)){
            arr.push(yxdata.yxpfyy_fe); //给数组arr追加一个值
            yxdata.yxpfyy_fe = arr;   //这句重新更改yxpfyy_fe的数据格式，做成数组
          }

          that.setData({
            yxdata: yxdata,
          })
        }
        
      },
      fail: function (res) {
        wx.showToast({
          title: '网络错误',
          icon: 'error'
        })
      },
      complete: function (res) {
      },
    })
  },
  changeCate(e){
    let cateid = e.currentTarget.dataset.cateid;
    this.setData({
      cateId: cateid
    });
    this.getDetail();
  },
  play(e){
    let url = e.currentTarget.dataset.url;
    if(url.indexOf("https:") == -1){
      this.audio.src = 'https:' + url;
    }else{
      this.audio.src = url;
    }
    // this.audio.autoplay = true;
    
    // console.log(this.audio.src)
    // this.setData({
    //   src:url
    // })
    if(this.audio.autoplay){
      // console.log(this.audio.autoplay)
      this.audio.autoplay = false
      this.audio.pause()
    }else{
      this.audio.autoplay = true
      this.audio.play()
    }
    // this.audio.play();
    

  }
})